package com.liu.qqRobot.climbingTower.handler;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.common.utils.HtmlToImageUtils;
import com.liu.common.utils.QQClientUtils;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerArticleInfo;
import com.liu.mybatisPlus.climbingTower.entity.base.ClimbingTowerUserArticle;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerArticleInfoService;
import com.liu.mybatisPlus.climbingTower.service.ClimbingTowerUserArticleService;
import com.liu.mybatisPlus.q.service.QGroupUserOperationQuantityService;
import com.liu.qqRobot.climbingTower.entity.dto.ClimbingTowerMyBackpackDTO;
import com.liu.qqRobot.event.entity.dto.EventReportingDTO;
import com.liu.qqRobot.q.enums.OperationTypeEnum;
import freemarker.template.TemplateException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Component
public class ClimbingTowerMyBackpackHandler {

    @Resource
    private ClimbingTowerUserArticleService climbingTowerUserArticleService;

    @Resource
    private ClimbingTowerArticleInfoService climbingTowerArticleInfoService;

    @Resource
    private QGroupUserOperationQuantityService qGroupUserOperationQuantityService;

    /**
     * 处理指令
     * @param data data
     * @param groupId groupId
     * @param userId userId
     * @param isGroup isGroup
     */
    public void handler(EventReportingDTO.Message.Data data, Long groupId, Long userId, Boolean isGroup) {
        QueryWrapper<ClimbingTowerUserArticle> userArticleQueryWrapper = new QueryWrapper<>();
        userArticleQueryWrapper.eq("group_id", groupId);
        userArticleQueryWrapper.eq("user_id", userId);
        userArticleQueryWrapper.gt("quantity", 0);
        List<ClimbingTowerUserArticle> climbingTowerUserArticleList = climbingTowerUserArticleService.list(userArticleQueryWrapper);
        List<Integer> articleIdList = climbingTowerUserArticleList.stream().map(ClimbingTowerUserArticle::getArticleId).collect(Collectors.toList());
        List<ClimbingTowerArticleInfo> climbingTowerArticleInfoList = climbingTowerArticleInfoService.listByIds(articleIdList);
        Map<Integer, String> articleNameMap = climbingTowerArticleInfoList.stream().collect(Collectors.toMap(ClimbingTowerArticleInfo::getId, ClimbingTowerArticleInfo::getName));
        List<ClimbingTowerMyBackpackDTO> climbingTowerMyBackpackDTOList = new ArrayList<>();
        climbingTowerUserArticleList.forEach(c -> {
            ClimbingTowerMyBackpackDTO climbingTowerMyBackpackDTO = new ClimbingTowerMyBackpackDTO();
            climbingTowerMyBackpackDTO.setName(articleNameMap.get(c.getArticleId()));
            climbingTowerMyBackpackDTO.setQuantity(c.getQuantity());
            climbingTowerMyBackpackDTOList.add(climbingTowerMyBackpackDTO);
        });
        Map<String, Object> templateParam = new HashMap<>();
        templateParam.put("climbingTowerMyBackpackDTOList", climbingTowerMyBackpackDTOList);
        try {
            HtmlToImageUtils.htmlToImage("climbingTowerMyBackpackTemplate.html", templateParam,
                    "src/main/resources/picture/climbingTowerMyBackpack.png", 1500);
            File file = new File("src/main/resources/picture/climbingTowerMyBackpack.png");
            String encode = Base64.encode(file);
            String msg = "[CQ:at,qq=" + userId + "]\n[CQ:image,file=base64://" + encode+ ",type=show,subType=0]";
            QQClientUtils.sendMsg(msg, userId, groupId, isGroup);
            qGroupUserOperationQuantityService.statisticsOperationQuantityByType(groupId, userId, OperationTypeEnum.CLIMBING_TOWER_MY_BACKPACK);
        } catch (IOException | ParserConfigurationException | SAXException | TemplateException e) {
            log.error("获取爬塔我的背包展示模板失败:{}", e.getMessage(), e);
        }
    }
}
